import { reqDeleteShopCartGood, reqEditShopCartNums, reqGetShopCart, reqUpdateChecked } from "@/api";

const state = {
    goodsList: [],
    totalNums: 0,
    totalPrice: 0
};
const actions = {
    // 获取购物车列表数据
    async getShopCart({ commit }, params) {
        let result = await reqGetShopCart(params);
        if (result.code == 200) {
            commit("GETSHOPCART", result.data);
            return 'ok';
        } else {
            return Promise.reject(result.msg);
        }
    },
    // 修改商品的选中状态
    async updateChecked({ commit }, params) {
        let result = await reqUpdateChecked(params)
        if (result.code == 200) {
            return 'ok'
        } else {
            return Promise.reject(result.msg);
        }
    },
    // 修改购物车商品的数量
    async editShopCartNums({ commit }, params) {
        let result = await reqEditShopCartNums(params);
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(result.msg)
        }
    },
    // 删除购物车的项目
    async deleteShopCartGoods({ commit }, goodId) {
        let result = await reqDeleteShopCartGood(goodId);
        if(result.code == 200){
            return 'ok';
        }else{
            return Promise.reject(result.msg);
        }
    }
};
const mutations = {
    GETSHOPCART(state, { totalNums, totalPrice, goodsList }) {
        state.goodsList = goodsList;
        state.totalNums = totalNums;
        state.totalPrice = totalPrice;
    }
};
const getters = {
    tradeList(state){
        return state.goodsList.filter(item=>item.checked == 1);
    }
}

export default {
    namespaced: true,
    state,
    actions,
    mutations,
    getters
}